package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.pojo.TbGoodsType;
import com.qf.utils.DBUtils;
import com.qf.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<TbGoodsType> findAllSecondLevelType() throws SQLException {
        String sql = SqlUtils.getSql("type");
        List<TbGoodsType> query = queryRunner.query(DBUtils.getConnection(), sql, new BeanListHandler<>(TbGoodsType.class));
        return query;
    }
    @Override
    public void addType(String name, String parent) throws SQLException {
        String sql = "INSERT INTO tb_goods_type (name,level,Parent) VALUES(?,0,?)";
        queryRunner.update(DBUtils.getConnection(),sql, name, parent);
    }

    @Override
    public int deleteType(String id) {
        String sql = "UPDATE tb_goods_type SET status= 0 WHERE id =?";
        try {
            return queryRunner.update(DBUtils.getConnection(),sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<TbGoodsType> findIdByPIdAndStaus(String pid, int status) {
        String sql = "SELECT id FROM tb_goods_type WHERE Parent=? AND status=?";
        List<TbGoodsType> typeList = null;
        try {
            typeList = queryRunner.query(DBUtils.getConnection(),sql, new BeanListHandler<>(TbGoodsType.class), pid, status);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return typeList;
    }
}
